package com.gk.carshopsys.controller;

import com.alibaba.excel.EasyExcel;
import com.gk.carshopsys.annotation.Log;
import com.gk.carshopsys.pojo.Car;
import com.gk.carshopsys.pojo.Part;
import com.gk.carshopsys.service.CarService;
import com.gk.carshopsys.service.PartService;
import com.gk.carshopsys.utils.CarDataListener;
import com.gk.carshopsys.utils.ExcelUtils;
import com.gk.carshopsys.utils.PartDataListener;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class PartController {
    @Autowired
    private PartService partService;

    //Excel参数
    private HashMap<String,String> param;


    //excel传参
    @PostMapping("/partlist/excelparamset")
    @ResponseBody
    public void excelset(@RequestBody HashMap<String,String> params){
        param= params;
    }

    //excel下载
    @Log("导出配件库存")
    @GetMapping("/partlist/excelexport")
    @ResponseBody
    public String excelexport(HttpServletResponse response) throws Exception {
        //导出的参数
        String id_s = param.get("id");
        String pname = param.get("pname");
        String factory = param.get("factory");
        String source = param.get("source");

        String low_price = param.get("low_price");//需要转型
        String hight_price = param.get("hight_price");//需要转型

        Part part = new Part();
        if(id_s!=null && !id_s.equals("")){
            try {
                Long id = Long.valueOf(id_s);
                part.setId(id);
            }catch (ClassCastException e){
            }finally {
                part.setPname(pname);
                part.setFactory(factory);
                part.setSource(source);
            }

        }
        part.setPname(pname);
        part.setFactory(factory);
        part.setSource(source);


        ExcelUtils.export2Web(response, "配件库存表", "配件库存信息",Part.class,partService.findPart(part,low_price,hight_price));
        return "success";
    }


    // easyexcel上传文件
    @Log("导入配件库存")
    @PostMapping("/partlist/excelimport")
    public String upload(MultipartFile file, RedirectAttributesModelMap model) throws IOException {
        EasyExcel.read(file.getInputStream(), Part.class, new PartDataListener(partService)).sheet().doRead();
        return "redirect:/partlist";
    }










    @RequestMapping("/partlist")
    public String topartlist(){
        return "partlist";
    }

    //查询配件
    @Log("查询配件库存")
    @RequestMapping("/partlist/query")
    @ResponseBody
    public  List<Part> queryPart(@RequestBody HashMap<String,String> params){
        String id_s = params.get("id");
        String pname = params.get("pname");
        String factory = params.get("factory");
        String source = params.get("source");

        String low_price = params.get("low_price");//需要转型
        String hight_price = params.get("hight_price");//需要转型

        Part part = new Part();
        if(id_s!=null && !id_s.equals("")){
            try {
                Long id = Long.valueOf(id_s);
                part.setId(id);
            }catch (ClassCastException e){
            }finally {
                part.setPname(pname);
                part.setFactory(factory);
                part.setSource(source);
            }

        }
        part.setPname(pname);
        part.setFactory(factory);
        part.setSource(source);


        return partService.findPart(part,low_price,hight_price);
    }

    //添加配件
    @Log("添加配件库存")
    @RequestMapping("/partlist/add")
    public String addPart(@RequestParam("add_pname") String pname,
                          @RequestParam("add_quantity") Long quantity,
                          @RequestParam("add_unit") String unit,
                          @RequestParam("add_price") Double  price,
                          @RequestParam("add_factory") String factory,
                          @RequestParam("add_source") String source,
                          RedirectAttributesModelMap model){


        Part part = new Part(pname,quantity,unit,price,factory,source);
        int result = partService.addPart(part);
        if (result==1){
            model.addFlashAttribute("updatemsg","添加成功");
            return "redirect:/partlist";
        }else {
            model.addFlashAttribute("updatemsg","添加成功，未知错误");
            return "redirect:/partlist";
        }

    }
    //更新配件
    @Log("修改配件库存")
    @RequestMapping("/partlist/update")
    public String updatePart(@RequestParam("update_id") Long id,
                          @RequestParam("update_pname") String pname,
                          @RequestParam("update_quantity") Long quantity,
                          @RequestParam("update_unit") String unit,
                          @RequestParam("update_price") Double  price,
                          @RequestParam("update_factory") String factory,
                          @RequestParam("update_source") String source,
                          RedirectAttributesModelMap model){

        Part part = new Part(id,pname,quantity,unit,price,factory,source);
        int result=partService.updatePart(part);
        if (result==1){
            model.addFlashAttribute("updatemsg","修改成功");
            return "redirect:/partlist";
        }else {
            model.addFlashAttribute("updatemsg","修改失败，未知错误");
            return "redirect:/partlist";
        }

    }

    //批量删除配件
    @Log("删除配件库存")
    @ResponseBody
    @RequestMapping("/partlist/del")
    public Map<String,Integer> partlistdel(@RequestBody List<Integer> ids){
        int result = partService.delPart(ids);
        Map<String,Integer> map = new HashMap<>();
        map.put("code",result);
        return map;
    }
}
